package com.itheima.service.impl;

import com.itheima.domain.Product;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.ProductService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
    List<Product> list =new ArrayList<>();
    Product product =new Product();
    InputStream is;
    SqlSession sqlSession;
    Integer result;
    Integer id;
    @Override
    public List<Product> findAll() {
        try {
            //1.加载核心配置文件
             is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
             sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            list = mapper.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public Product findById(Integer id) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            product = mapper.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return product;
    }

    @Override
    public int add(Product product) {
        Integer result =null;
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
           // result = sqlSession.insert("ProductMapper.addById", product);
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
             result=mapper.add(product);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int updateById(Product product) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            id = product.getId();
            result = mapper.updateById(product);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int deleteById(Integer id) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            result = mapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int deleteByIdStatus(Integer id) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            result = mapper.deleteByIdStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public List<Product> findByNPT(Product product) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
           list = mapper.findByNPT(product);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public int updateCon(Product product) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            id=product.getId();
            result = mapper.updateCon(product);
            System.out.println( result);
            result =result;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int addBatch(List<Product> list) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SQLSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接受结果
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            result = mapper.addBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
